home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 43 / Amiga Format CD43 (1999)(Future Publishing)(GB)(Track 1 of 2)[!][issue 1999-09].iso / -serious- / programming / other / python-1.52 / lib / python1.5 / colorsys.py < prev    next >
Text File  |  1999-06-14  |  3KB  |  122 lines

  1. """Conversion functions between RGB and other color systems.
  2.  
  3. This modules provides two functions for each color system ABC:
  4.  
  5.   rgb_to_abc(r, g, b) --> a, b, c
  6.   abc_to_rgb(a, b, c) --> r, g, b
  7.  
  8. All inputs and outputs are triples of floats in the range [0.0...1.0].
  9. Inputs outside this range may cause exceptions or invalid outputs.
  10.  
  11. Supported color systems:
  12. RGB: Red, Green, Blue components
  13. YIQ: used by composite video signals
  14. HLS: Hue, Luminance, S???
  15. HSV: Hue, Saturation, Value(?)
  16. """
  17. # References:
  18. # XXX Where's the literature?
  19.  
  20.  
  21. # Some floating point constants
  22.  
  23. ONE_THIRD = 1.0/3.0
  24. ONE_SIXTH = 1.0/6.0
  25. TWO_THIRD = 2.0/3.0
  26.  
  27.  
  28. # YIQ: used by composite video signals (linear combinations of RGB)
  29. # Y: perceived grey level (0.0 == black, 1.0 == white)
  30. # I, Q: color components
  31.  
  32. def rgb_to_yiq(r, g, b):
  33.     y = 0.30*r + 0.59*g + 0.11*b
  34.     i = 0.60*r - 0.28*g - 0.32*b
  35.     q = 0.21*r - 0.52*g + 0.31*b
  36.     return (y, i, q)
  37.  
  38. def yiq_to_rgb(y, i, q):
  39.     r = y + 0.948262*i + 0.624013*q
  40.     g = y - 0.276066*i - 0.639810*q
  41.     b = y - 1.105450*i + 1.729860*q
  42.     if r < 0.0: r = 0.0
  43.     if g < 0.0: g = 0.0
  44.     if b < 0.0: b = 0.0
  45.     if r > 1.0: r = 1.0
  46.     if g > 1.0: g = 1.0
  47.     if b > 1.0: b = 1.0
  48.     return (r, g, b)
  49.  
  50.  
  51. # HLS: Hue, Luminance, S???
  52. # H: position in the spectrum
  53. # L: ???
  54. # S: ???
  55.  
  56. def rgb_to_hls(r, g, b):
  57.     maxc = max(r, g, b)
  58.     minc = min(r, g, b)
  59.     # XXX Can optimize (maxc+minc) and (maxc-minc)
  60.     l = (minc+maxc)/2.0
  61.     if minc == maxc: return 0.0, l, 0.0
  62.     if l <= 0.5: s = (maxc-minc) / (maxc+minc)
  63.     else: s = (maxc-minc) / (2.0-maxc-minc)
  64.     rc = (maxc-r) / (maxc-minc)
  65.     gc = (maxc-g) / (maxc-minc)
  66.     bc = (maxc-b) / (maxc-minc)
  67.     if r == maxc: h = bc-gc
  68.     elif g == maxc: h = 2.0+rc-bc
  69.     else: h = 4.0+gc-rc
  70.     h = (h/6.0) % 1.0
  71.     return h, l, s
  72.  
  73. def hls_to_rgb(h, l, s):
  74.     if s == 0.0: return l, l, l
  75.     if l <= 0.5: m2 = l * (1.0+s)
  76.     else: m2 = l+s-(l*s)
  77.     m1 = 2.0*l - m2
  78.     return (_v(m1, m2, h+ONE_THIRD), _v(m1, m2, h), _v(m1, m2, h-ONE_THIRD))
  79.  
  80. def _v(m1, m2, hue):
  81.     hue = hue % 1.0
  82.     if hue < ONE_SIXTH: return m1 + (m2-m1)*hue*6.0
  83.     if hue < 0.5: return m2
  84.     if hue < TWO_THIRD: return m1 + (m2-m1)*(TWO_THIRD-hue)*6.0
  85.     return m1
  86.  
  87.  
  88. # HSV: Hue, Saturation, Value(?)
  89. # H: position in the spectrum
  90. # S: ???
  91. # V: ???
  92.  
  93. def rgb_to_hsv(r, g, b):
  94.     maxc = max(r, g, b)
  95.     minc = min(r, g, b)
  96.     v = maxc
  97.     if minc == maxc: return 0.0, 0.0, v
  98.     s = (maxc-minc) / maxc
  99.     rc = (maxc-r) / (maxc-minc)
  100.     gc = (maxc-g) / (maxc-minc)
  101.     bc = (maxc-b) / (maxc-minc)
  102.     if r == maxc: h = bc-gc
  103.     elif g == maxc: h = 2.0+rc-bc
  104.     else: h = 4.0+gc-rc
  105.     h = (h/6.0) % 1.0
  106.     return h, s, v
  107.  
  108. def hsv_to_rgb(h, s, v):
  109.     if s == 0.0: return v, v, v
  110.     i = int(h*6.0) # XXX assume int() truncates!
  111.     f = (h*6.0) - i
  112.     p = v*(1.0 - s)
  113.     q = v*(1.0 - s*f)
  114.     t = v*(1.0 - s*(1.0-f))
  115.     if i%6 == 0: return v, t, p
  116.     if i == 1: return q, v, p
  117.     if i == 2: return p, v, t
  118.     if i == 3: return p, q, v
  119.     if i == 4: return t, p, v
  120.     if i == 5: return v, p, q
  121.     # Cannot get here
  122.